package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;
import com.duobao.wuping.Hunmi;

import java.awt.event.*;
import java.lang.reflect.*;

public class Wuping extends JPanel implements MouseListener{

    DuobaoSwingLaunch game;
    Map heroMap;
    Equip equip;
    JLabel fanhui;
    JLabel fanhuiyouxi;

    JLabel msName;
    JLabel describe[];
    JLabel gongjili;
    JLabel fangyuli;
    JLabel shangduo;
    JLabel naijiudu;
    JLabel zhanxindu;

    JLabel tuoxia;
    JLabel soushen;
    JLabel fangxia;
    JLabel chuandai;
    JLabel zhuangpei;
    JLabel xuexi;
    JLabel rengqi;

    public Wuping(DuobaoSwingLaunch game, Equip equip)
    {

        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);

        this.equip = equip;

        setMsName();
        setDescribe();
        setShuxing();
        setFanhui();
        setFanhuiyouxi();
    }

    public void setMsName() {
        msName=new JLabel(this.equip.getMsName());
        msName.setFont(myFont.f2);
        msName.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(msName);
        this.add(jp1);
    }

    public void setDescribe() {
        String Des[]=SplitString.splitString(this.equip.getDescribe());
        int leng=Des.length;
        describe=new JLabel[leng];

        for(int i=0;i<leng;i++)
        {
            describe[i]=new JLabel();
            describe[i].addMouseListener(this);
            describe[i].setFont(myFont.f2);
            describe[i].setText(Des[i]);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(describe[i]);
            this.add(jp1);
        }
    }
    public void setShuxing()
    {
        if(this.equip.ifShuji())
        {
            this.setXuexi();
            this.setRengqi();
        }else if(this.equip.ifqita())
        {
            this.setRengqi();
        }else if(this.equip.ifhunmi())
        {
            this.setsoushen();
            this.setRengqi();
        }else if(this.equip.ifHuoBi())
        {
            this.setRengqi();
        }else
        {
            this.setGongjili();
            this.setFangyuli();
            this.setShangduo();
            this.setNaijiudu();
            this.setZhanxindu();
            if(this.equip.isZB())
            {
                if(this.equip.ifWuqi())
                {
                    this.setFangxia();
                }else
                {
                    this.setTuoxia();
                }
            }else
            {
                if(this.equip.ifWuqi())
                {
                    this.setZhuangpei();
                }else
                {
                    this.setChuandai();
                }
                this.setRengqi();
            }
        }
    }
    public void setGongjili() {
        int atk=equip.getATK();
        if(atk!=0)
        {
            gongjili=new JLabel("攻击力:"+this.equip.getATK());
            gongjili.setFont(myFont.f2);
            gongjili.addMouseListener(this);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(gongjili);
            this.add(jp1);
        }

    }

    public void setFangyuli() {
        int def=equip.getDEF();
        if(def!=0)
        {
            fangyuli=new JLabel("防御力:"+this.equip.getDEF());
            fangyuli.setFont(myFont.f2);
            fangyuli.addMouseListener(this);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(fangyuli);
            this.add(jp1);
        }

    }
    public void setShangduo()
    {
        int dog=equip.getDodge();
        if(dog!=0)
        {
            shangduo=new JLabel("闪躲力:"+this.equip.getDodge());
            shangduo.setFont(myFont.f2);
            shangduo.addMouseListener(this);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(shangduo);
            this.add(jp1);
        }
    }
    public void setNaijiudu() {
        naijiudu=new JLabel("耐久度:"+this.equip.getEndurance()+"/"+this.equip.getMaxendurance());
        naijiudu.setFont(myFont.f2);
        naijiudu.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(naijiudu);
        this.add(jp1);
    }

    public void setZhanxindu( ) {
        String Recency ="";
        for(int i=0;i<this.equip.getRecency();i++)
        {
            Recency = Recency + "*";
        }
        zhanxindu=new JLabel("崭新度:"+Recency);
        zhanxindu.setFont(myFont.f2);
        zhanxindu.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(zhanxindu);
        this.add(jp1);
    }
    public void setsoushen() {

        soushen=new JLabel();
        soushen.addMouseListener(this);
        soushen.setFont(myFont.f2);
        soushen.setForeground(Color.BLUE);
        soushen.setText("<html><u>"+"搜身"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(soushen);
        this.add(jp3);
    }
    public void setTuoxia() {

        tuoxia=new JLabel();
        tuoxia.addMouseListener(this);
        tuoxia.setFont(myFont.f2);
        tuoxia.setForeground(Color.BLUE);
        tuoxia.setText("<html><u>"+"脱下"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(tuoxia);
        this.add(jp3);
    }
    public void setFangxia() {

        fangxia=new JLabel();
        fangxia.addMouseListener(this);
        fangxia.setFont(myFont.f2);
        fangxia.setForeground(Color.BLUE);
        fangxia.setText("<html><u>"+"放下"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fangxia);
        this.add(jp3);
    }
    public void setChuandai() {

        chuandai=new JLabel();
        chuandai.addMouseListener(this);
        chuandai.setFont(myFont.f2);
        chuandai.setForeground(Color.BLUE);
        chuandai.setText("<html><u>"+"穿戴"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(chuandai);
        this.add(jp3);
    }
    public void setZhuangpei() {

        zhuangpei=new JLabel();
        zhuangpei.addMouseListener(this);
        zhuangpei.setFont(myFont.f2);
        zhuangpei.setForeground(Color.BLUE);
        zhuangpei.setText("<html><u>"+"装配"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(zhuangpei);
        this.add(jp3);
    }

    public void setXuexi() {
        xuexi=new JLabel();
        xuexi.addMouseListener(this);
        xuexi.setFont(myFont.f2);
        xuexi.setForeground(Color.BLUE);
        xuexi.setText("<html><u>"+"研读"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(xuexi);
        this.add(jp3);
    }

    public void setRengqi( ) {
        rengqi=new JLabel();
        rengqi.addMouseListener(this);
        rengqi.setFont(myFont.f2);
        rengqi.setForeground(Color.BLUE);
        rengqi.setText("<html><u>"+"扔弃"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(rengqi);
        this.add(jp3);
    }

    private void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.add(jp3);
    }

    private void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.add(jp4);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==msName || jl== gongjili || jl== fangyuli || jl== shangduo || jl==naijiudu || jl==zhanxindu)
        {
            f=true;
        }else
        {
            for(int i=0;i<describe.length;i++)
            {
                if(jl==describe[i])
                {
                    f=true;
                }
            }
        }
        return f;
    }
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==this.chuandai)
        {
            this.game.setPanel(new ChuanDai(this.game,this.equip));
        }else if(e.getSource()==this.zhuangpei)
        {
            this.game.setPanel(new ZhuangPei(this.game,this.equip));
        }else if(e.getSource()==this.soushen)
        {
            Hunmi hm=(Hunmi)this.equip;
            String str=hm.sousheng(this.game.hero);
            if(str.equals(""))
            {
                str="你什么都没搜到！";
            }else
            {
                str="你搜到了"+str+"。";
            }
            this.game.setPanel(new SoushengTishi(this.game,str,this.equip));
        }else if(e.getSource()==this.xuexi)
        {
            this.game.setPanel(new Yandu(this.game,this.equip));
        }else if(e.getSource()==this.fangxia || e.getSource()==this.tuoxia)
        {
            this.game.setPanel(new FangTuoXia(this.game,this.equip));
        }else if(e.getSource()==rengqi)
        {
            if(!this.equip.ifHuoBi())
            {
                this.game.setPanel(new Diuqi(this.game,this.equip));
            }else
            {
                this.game.setPanel(new Diuqihuobi(this.game,this.equip));
            }

        }else if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            try {
                this.game.setPanel(new Shuishen(this.game,ZhuangTaiPanel.class.getDeclaredConstructor(DuobaoSwingLaunch.class,Constructor.class,int.class),1));
            }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }

    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}
